﻿using d1Common.WebController;
using d1Project.Modules.Business.doXZ.Models;
using d1Project.Tools;
using d1Project.Tools.PagedList;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;

namespace d1Project.Modules.Business.doXZ.Controllers.a1
{
    /// <summary>
    /// 企业类型
    /// </summary>
    [Route("a1/a1_Enterprise")]
    public class a1_EnterpriseController : ControllerBase
    {
        protected override DbContext dbContext
        {
            get
            {
                return this.db;
            }
        }
        private doXZDbContext db = new doXZDbContext();

        /// <summary>
        /// 企业信息列表/下拉显示
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Get(XZ_QyTypeModel_GetVm model)
        {
            var entity = db.XZ_QyType.AsQueryable();
            if (model != null)
            {

            }
            var result = await entity.OrderByDescending(p => p.Id).ToPagedListAsync(model);
            return Ok(new { total = entity.Count(), rows = result });
        }

        /// <summary>
        /// 企业类型添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Post(XZ_QyTypeModel_PostVm model)
        {
            if (!ModelState.IsValid) this.Error("请求参数不正确");
            XZ_QyType entity = new XZ_QyType();
            
            entity.Name = model.Name;
            entity.Gws = model.Gws;
            entity.Zws = model.Zws;
            entity.Dws = model.Dws;
            entity.CreateTime = DateTime.Now;
            entity.UserId = "1";
            db.XZ_QyType.Add(entity);
            await db.SaveChangesAsync();
            return Ok(new { name = 1 });
        }

        /// <summary>
        /// 删除企业类型
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<IHttpActionResult> Delete(int id)
        {
            var entity = await db.XZ_QyType.FindAsync(id);
            db.XZ_QyType.Remove(entity);
            await db.SaveChangesAsync();
            return Ok(new { name = 1 });
        }
    }
    public class XZ_QyTypeModel_GetVm : VmBase
    {

    }

    public class XZ_QyTypeModel_PostVm : VmBase
    {
        /// <summary>
        /// 企业名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 高位数
        /// </summary>
        public double Gws { get; set; }
        /// <summary>
        /// 中位数
        /// </summary>
        public double Zws { get; set; }
        /// <summary>
        /// 地位数
        /// </summary>
        public double Dws { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }
        /// <summary>
        /// 用户编号
        /// </summary>
        public string UserId { get; set; }
    }
}